package com.etennis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etennis.constants.AppHttpCodeEnum;
import com.etennis.constants.IOEnum;
import com.etennis.exception.SystemException;
import com.etennis.mapper.ClubMapper;
import com.etennis.mapper.NoticeMapper;
import com.etennis.pojos.PageResult;
import com.etennis.pojos.dto.web.PublicityDTO;
import com.etennis.pojos.dto.web.PublicityPageQueryDTO;
import com.etennis.pojos.entity.Club;
import com.etennis.pojos.entity.Notice;
import com.etennis.pojos.vo.web.PublicityVO;
import com.etennis.service.INoticeService;
import com.etennis.util.SecurityUtils;
import com.etennis.utils.BeanCopyUtils;
import com.etennis.utils.IOUtils;
import com.etennis.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

import static com.etennis.constants.IconConstant.NOTICE_ICON;

@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {
    @Autowired
    NoticeMapper noticeMapper;
    @Autowired
    ClubMapper clubMapper;
    @Autowired
    IOUtils ioUtils;

    @Override
    public void savePublicity(PublicityDTO publicityDTO) {
        Long clubId = SecurityUtils.getLoginClubId();
        if(ioUtils.getType(publicityDTO.getPicture().getOriginalFilename())!="0")
            throw  new SystemException(AppHttpCodeEnum.MEDIA_TYPE_ERROR);
        Notice notice = null;
        try {
            String url = ioUtils.uploadMedia(publicityDTO.getPicture(), IOEnum.IS_NOTICE);
            Date releaseTime = new Date();
            notice = BeanCopyUtils.copyBean(publicityDTO, Notice.class);
            notice.setUrl(url);
            notice.setClubId(clubId);
            notice.setReleaseTime(releaseTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(Objects.isNull(notice.getUrl())){
            notice.setUrl(NOTICE_ICON);
        }
        noticeMapper.insert(notice);
    }

    @Override
    public void updatePublicity(PublicityDTO publicityDTO) {
        Notice notice = BeanCopyUtils.copyBean(publicityDTO, Notice.class);
        noticeMapper.updateById(notice);
    }

    @Override
    public PageResult<PublicityVO> pagePublicity(PublicityPageQueryDTO pageQueryDTO) {
        Page<Notice> page = new Page<>(pageQueryDTO.getPage(),pageQueryDTO.getPageSize());
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        if(pageQueryDTO.getTitle()!=null&&pageQueryDTO.getTitle().length()!=0) {
            queryWrapper.eq(Notice::getTitle,pageQueryDTO.getTitle());
        }
        Page<Notice> noticePage = noticeMapper.selectPage(page, queryWrapper);
        return PageUtil.convert(noticePage,each->{
            PublicityVO publicityVO=BeanCopyUtils.copyBean(each,PublicityVO.class);
            Long clubId = each.getClubId();
            //List<Club> clubs = clubMapper.selectBatchIds(Collections.singleton(clubId));
            Club club = clubMapper.selectById(clubId);
            String name = club.getName();
            publicityVO.setClubName(name);
            return publicityVO;
        });
    }
}
